<?php
require_once("mysql.inc.php");
require('fpdf.php');

if(isset($_GET["a"])) {

	switch ($_GET["a"]) {
	
		case "get":
			getIdsToPrint();
		break;
		
		case "print":
			printIds();
		break;
		
		case "add":
			add();
		break;
	}
}

function printIds() {

	class PDF extends FPDF
	{
		function Footer()
		{
			//Position at 1.5 cm from bottom
			$this->SetY(-10);
			//Arial italic 8
			$this->SetFont('Arial','I',8);
			$this->Cell(0,10,'Deze pagina werd gegenereert met Kebo ' . date("d/m/Y H:i"),0,0,'C');
		}
	}

	if(!isset($_SESSION)) {
		session_start();
	}
	
	//Instanciation of inherited class
	$pdf = new PDF();
	$pdf->AliasNbPages();
	$pdf->AddPage();
	if(!isset($_SESSION)) {
				session_start();
	}
	if(!isset($_SESSION["num"])) {
		echo 0;
		die();
	}
	
	$num = $_SESSION["num"];
	
	for($i=1; $i<=$num; $i++) {
		$id = mysql_real_escape_string($_SESSION["id$i"]);
		$result = mysql_query("SELECT * FROM `books` WHERE `id`='$id'") or die (mysql_error());
		$fetch = mysql_fetch_array($result);
		
		$line1 = substr($fetch["author"], 0, 8) . ". / " . substr($fetch["title"], 0, 12) . ".";
		$line2 = "ID: " . $id;
		
		//Arial bold 15
		$pdf->SetFont('Arial','B',11);
		$pdf->Cell(50,10,$line1, 1, 2, 'L');
		$pdf->Cell(50,10,$line2, 1, 2, 'L');
		$pdf->SetFont('Arial','I',10);
		$pdf->Cell(50,10,'Behandel mij met zorg aub!', 1, 2, 'L');
		
		$pdf->SetXY($pdf->GetX() + 70, $pdf->GetY() - 30);
		
		if(is_int($i / 3)) {
			$pdf->SetXY($pdf->GetX() - 210, $pdf->GetY() + 50);
		}	
	}
	
	$pdf->Output();
	
	$num = $_SESSION["num"];
	
	for($i=1; $i<=$num; $i++) {
		unset($_SESSION["id$i"]);
	}
	
	unset($_SESSION["num"]);
}



function getIdsToPrint() {
	
	if(!isset($_SESSION)) {
		session_start();
	}
	if(!isset($_SESSION["num"])) {
		echo 0;
		die();
	}
	else {
		$num = $_SESSION["num"];
	}
	
	//create new dom document
	$doc = new DomDocument('1.0');
	$root = $doc->createElement("results");
	$doc->appendChild($root);
	
	$no_info = "Niet bekend";

	//create first element containing amount of results
	$n = $doc->createElement("num");
	$n->appendChild($doc->createTextNode($num));
	$root->appendChild($n);
	
	for($i=1; $i<=$num; $i++) {
		
		$id = mysql_real_escape_string($_SESSION["id$i"]);
		$result = mysql_query("SELECT * FROM `books` WHERE `id`='$id'") or die(mysql_error());
		$fetch = mysql_fetch_array($result);
		
		$t = $doc->createElement("book$i");
		
		$title = $doc->createElement("title$i");
		if(!empty($fetch["title"])) {
			$title->appendChild($doc->createTextNode($fetch["title"]));
		}			
		else {
			$title->appendChild($doc->createTextNode($no_info));
		}		
		$t->appendChild($title);
		
		$author = $doc->createElement("author$i");
		if(!empty($fetch["author"])) {
			$author->appendChild($doc->createTextNode($fetch["author"]));
		}
		else {
			$author->appendChild($doc->createTextNode($no_info));
		}		
		$t->appendChild($author);
		
		$id = $doc->createElement("id$i");
		$id->appendChild($doc->createTextNode($fetch["id"]));
		$t->appendChild($id);
				
		$root->appendChild($t);
	}
	
	echo $doc->saveXML();
}

function add() {
	
	if(!isset($_SESSION)) {
		session_start();
	}
	if(!isset($_SESSION["username"])) {
		echo 1;
		die();
	}
	
	if(!isset($_GET["id"])) {
		
		$author = mysql_real_escape_string($_GET["author"]);
		$title = mysql_real_escape_string($_GET["title"]);
		
		$result = mysql_query("SELECT MIN(id) FROM `books` WHERE `title`='$title' AND `author`='$author' ") or die(mysql_error());
		$fetch = mysql_fetch_array($result);	
		$id = $fetch[0];
	}
	else {
		$id = $_GET["id"];
	}
	
	if(!isset($_SESSION["num"])) {
		$_SESSION["num"] = 1;
	}
	elseif($_SESSION["num"] == 21) {
		echo 0;
		die();
	}
	else {
		$_SESSION["num"]++;
	}
	
	$n = $_SESSION["num"];
	
	$_SESSION["id$n"] = $id;
}
?>